home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / POSTSCPT / GSVIEW / SRC / GVCPRN.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-27  |  13.4 KB  |  523 lines

  1. /* Copyright (C) 1993, 1994, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of GSview.
  4.   
  5.   This program is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the GSview Free Public Licence 
  9.   (the "Licence") for full details.
  10.   
  11.   Every copy of GSview must include a copy of the Licence, normally in a 
  12.   plain ASCII text file named LICENCE.  The Licence grants you the right 
  13.   to copy, modify and redistribute GSview, but only under certain conditions 
  14.   described in the Licence.  Among other things, the Licence requires that 
  15.   the copyright notice and this notice be preserved on all copies.
  16. */
  17.  
  18. /* gvcprn.c */
  19. /* Printer module of PM and Windows GSview */
  20.  
  21. #ifdef _Windows
  22. #include "gvwin.h"
  23. #else
  24. #include "gvpm.h"
  25. #endif
  26.  
  27. struct prop_item_s *
  28. get_properties(char *device)
  29. {
  30. char *entries, *p, *q;
  31. int i, numentry;
  32. struct prop_item_s *proplist;
  33. PROFILE *prf;
  34.     entries = malloc(PROFILE_SIZE);
  35.     if (entries == (char *)NULL)
  36.        return NULL;
  37.     if ( (prf = profile_open(szIniFile)) == (PROFILE *)NULL) {
  38.         free(entries);
  39.         return NULL;
  40.     }
  41.     profile_read_string(prf, device, NULL, "", entries, PROFILE_SIZE);
  42. /*
  43.     if (strlen(entries) == 0) {
  44.         profile_close(prf);
  45.         free(entries);
  46.         return NULL;
  47.     }
  48. */
  49.     p = entries;
  50.     for (numentry=0; p!=(char *)NULL && strlen(p)!=0; numentry++)
  51.         p += strlen(p) + 1;
  52.     proplist = (struct prop_item_s *)malloc((numentry+1) * sizeof(struct prop_item_s));
  53.     if (proplist == (struct prop_item_s *)NULL) {
  54.         profile_close(prf);
  55.         free(entries);
  56.         return NULL;
  57.     }
  58.     p = entries;
  59.     for (i=0; i<numentry; i++) {
  60.         strcpy(proplist[i].name, p);
  61.         profile_read_string(prf, device, p, "", proplist[i].value, sizeof(proplist->value));
  62.         q = proplist[i].value;
  63.         while ((*q) && (q[strlen(q)-1]==' '))
  64.         q[strlen(q)-1] = '\0';    /* remove trailing spaces */
  65.         p += strlen(p) + 1;
  66.     }
  67.     proplist[numentry].name[0] = '\0';
  68.     proplist[numentry].value[0] = '\0';
  69.     profile_close(prf);
  70.     free(entries);
  71.     return proplist;
  72. }
  73.  
  74. char *
  75. get_devices()
  76. {
  77. char *p;
  78. PROFILE *prf;
  79.     if ( (prf = profile_open(szIniFile)) == (PROFILE *)NULL)
  80.         return (char *)NULL;
  81.  
  82.     if ( (p = malloc(PROFILE_SIZE)) == (char *)NULL) {
  83.         profile_close(prf);
  84.         return (char *)NULL;
  85.     }
  86.  
  87.     profile_read_string(prf, DEVSECTION, NULL, "", p, PROFILE_SIZE);
  88.     if (strlen(p) == 0) {
  89.         /* [Devices] section doesn't exist.  Initialise from resources */
  90.         profile_create_section(prf, DEVSECTION, IDR_DEVICES);
  91.     }
  92.     profile_read_string(prf, DEVSECTION, NULL, "", p, PROFILE_SIZE);
  93.     profile_close(prf);
  94.     return p;
  95. }
  96.  
  97.  
  98. /* get a filename and spool it for printing */
  99. void
  100. gsview_spool(char *fname, char *port)
  101. {
  102.     static char filename[MAXSTR];
  103.  
  104.     if (fname == (char *)NULL) {
  105.         if (!get_filename(filename, FALSE, FILTER_ALL, IDS_PRINTFILE, IDS_TOPICPRINT))
  106.         return;
  107.     }
  108.     else {
  109.         while (*fname && *fname==' ')
  110.             fname++;
  111.         strncpy(filename, fname, MAXSTR);
  112.     }
  113.  
  114.     if (!gp_printfile(filename, port)) {
  115.         play_sound(SOUND_ERROR);
  116.         return;
  117.     }
  118. }
  119.  
  120.  
  121. /* save entire file */
  122. /* added to save files when GSview used as a WWW viewer */
  123. void
  124. gsview_saveas()
  125. {
  126. FILE *f;
  127. char output[MAXSTR];
  128. FILE *infile;
  129. UINT count;
  130. char *buffer;
  131.  
  132.     output[0] = '\0';
  133.     if (psfile.name[0] == '\0') {
  134.         gserror(IDS_NOTOPEN, NULL, MB_ICONEXCLAMATION, SOUND_NOTOPEN);
  135.         return;
  136.     }
  137.  
  138.     load_string(IDS_TOPICOPEN, szHelpTopic, sizeof(szHelpTopic));
  139.     if (!get_filename(output, TRUE, FILTER_PS, 0, IDS_TOPICOPEN))
  140.         return;
  141.  
  142.     if ((f = fopen(output, "wb")) == (FILE *)NULL) {
  143.         return;
  144.     }
  145.  
  146.     /* create buffer for PS file copy */
  147.     buffer = malloc(COPY_BUF_SIZE);
  148.     if (buffer == (char *)NULL) {
  149.         play_sound(SOUND_ERROR);
  150.         fclose(f);
  151.         unlink(output);
  152.         return;
  153.     }
  154.  
  155.     infile = fopen(psfile.name, "rb");
  156.     if (infile == (FILE *)NULL) {
  157.         play_sound(SOUND_ERROR);
  158.         free(buffer);
  159.         fclose(f);
  160.         unlink(output);
  161.         return;
  162.     }
  163.  
  164.     info_wait(IDS_WAITWRITE);
  165.  
  166.         while ( (count = fread(buffer, 1, COPY_BUF_SIZE, infile)) != 0 ) {
  167.         fwrite(buffer, 1, count, f);
  168.     }
  169.     free(buffer);
  170.     fclose(infile);
  171.     fclose(f);
  172.  
  173.     info_wait(IDS_NOWAIT);
  174.     return;
  175. }
  176.  
  177. /* extract a range of pages for later printing */
  178. void
  179. gsview_extract()
  180. {
  181.     FILE *f;
  182.     static char output[MAXSTR];
  183.     int thispage = psfile.pagenum;
  184.  
  185.     if (psfile.name[0] == '\0') {
  186.         gserror(IDS_NOTOPEN, NULL, MB_ICONEXCLAMATION, SOUND_NOTOPEN);
  187.         return;
  188.     }
  189.  
  190.     if (doc == (PSDOC *)NULL) {
  191.         gserror(IDS_NOPAGE, NULL, MB_ICONEXCLAMATION, SOUND_NONUMBER);
  192.         return;
  193.     }
  194.     
  195.     load_string(IDS_TOPICOPEN, szHelpTopic, sizeof(szHelpTopic));
  196.     if (doc->numpages != 0)
  197.         if (!get_page(&thispage, TRUE))
  198.             return;
  199.  
  200.     if (!get_filename(output, TRUE, FILTER_PS, 0, IDS_TOPICOPEN))
  201.         return;
  202.  
  203.     if ((f = fopen(output, "wb")) == (FILE *)NULL) {
  204.         return;
  205.     }
  206.  
  207.     info_wait(IDS_WAITWRITE);
  208.     if (doc->numpages != 0)
  209.         psfile_extract(f);
  210.     else {
  211.         pscopyuntil(psfile.file, f, doc->beginheader, doc->endtrailer, NULL);
  212.     }
  213.  
  214.     fclose(f);
  215.  
  216.     info_wait(IDS_NOWAIT);
  217.     return;
  218. }
  219.  
  220.  
  221. /* Copy the headers, marked pages, and trailer to f */
  222. void
  223. psfile_extract(FILE *f)
  224. {
  225.     char text[PSLINELENGTH];
  226.     char *comment;
  227.     BOOL pages_written = FALSE;
  228.     BOOL pages_atend = FALSE;
  229.     int pages = 0;
  230.     int page;
  231.     int i;
  232.     long position;
  233.  
  234.     for (i=0; i< doc->numpages; i++) {
  235.         if (page_list.select[i]) pages++;
  236.     }
  237.  
  238.     position = doc->beginheader;
  239.     while ( (comment = pscopyuntil(psfile.file, f, position,
  240.                doc->endheader, "%%Pages:")) != (char *)NULL ) {
  241.     position = ftell(psfile.file);
  242.     if (pages_written || pages_atend) {
  243.         free(comment);
  244.         continue;
  245.     }
  246.     sscanf(comment+8, "%s", text);
  247.     if (strcmp(text, "(atend)") == 0) {
  248.         fputs(comment, f);
  249.         pages_atend = TRUE;
  250.     } else {
  251.         switch (sscanf(comment+8, "%*d %d", &i)) {
  252.         case 1:
  253.             fprintf(f, "%%%%Pages: %d %d\r\n", pages, i);
  254.             break;
  255.         default:
  256.             fprintf(f, "%%%%Pages: %d\r\n", pages);
  257.             break;
  258.         }
  259.         pages_written = TRUE;
  260.     }
  261.     free(comment);
  262.     }
  263.     pscopyuntil(psfile.file, f, doc->beginpreview, doc->endpreview, NULL);
  264.     pscopyuntil(psfile.file, f, doc->begindefaults, doc->enddefaults, NULL);
  265.     pscopyuntil(psfile.file, f, doc->beginprolog, doc->endprolog, NULL);
  266.     pscopyuntil(psfile.file, f, doc->beginsetup, doc->endsetup, NULL);
  267.  
  268.     page = 1;
  269.     for (i = 0; i < doc->numpages; i++) {
  270.     if (page_list.select[map_page(i)])  {
  271.         comment = pscopyuntil(psfile.file, f, doc->pages[i].begin,
  272.                   doc->pages[i].end, "%%Page:");
  273.         fprintf(f, "%%%%Page: %s %d\r\n",
  274.             doc->pages[i].label, page++);
  275.         free(comment);
  276.         pscopyuntil(psfile.file, f, -1, doc->pages[i].end, NULL);
  277.     }
  278.     }
  279.  
  280.     position = doc->begintrailer;
  281.     while ( (comment = pscopyuntil(psfile.file, f, position,
  282.                doc->endtrailer, "%%Pages:")) != (char *)NULL ) {
  283.     position = ftell(psfile.file);
  284.     if (pages_written) {
  285.         free(comment);
  286.         continue;
  287.     }
  288.     switch (sscanf(comment+8, "%*d %d", &i)) {
  289.         case 1:
  290.         fprintf(f, "%%%%Pages: %d %d\r\n", pages, i);
  291.         break;
  292.         default:
  293.         fprintf(f, "%%%%Pages: %d\r\n", pages);
  294.         break;
  295.     }
  296.     pages_written = TRUE;
  297.     free(comment);
  298.     }
  299. }
  300.  
  301. /* common printer code */
  302. BOOL
  303. gsview_cprint(BOOL to_file, char *psname, char *optname)
  304. {
  305. int i;
  306. float print_xdpi, print_ydpi;
  307. int width, height;
  308. struct prop_item_s *proplist;
  309. FILE *optfile;
  310. FILE *pcfile;
  311. char *p;
  312. static char output[MAXSTR]; /* output filename for printing */
  313. char section[MAXSTR];
  314. char buf[MAXSTR];
  315. float xoffset, yoffset;
  316. PROFILE *prf;
  317.  
  318. #ifdef OLD
  319.     fname = (char *)NULL;
  320.     if (doc == (PSDOC *)NULL) {
  321.         play_sound(SOUND_NONUMBER);
  322.         load_string(IDS_PRINTINGALL, buf, sizeof(buf));
  323.         if (message_box(buf, MB_ICONASTERISK) == IDCANCEL)
  324.             return FALSE;
  325.         fname = psfile.name;
  326.         pages = 1;
  327.     }
  328.     else {
  329.     pages = 1;
  330.     if (doc->numpages != 0) {
  331.         if (!get_page(&thispage, TRUE))
  332.         return FALSE;
  333.         pages = 0;
  334.         for (i=0; i< doc->numpages; i++) {
  335.         if (page_list.select[i]) pages++;
  336.         }
  337.     }
  338.  
  339.     if ((cfname[0] != '\0') && !debug)
  340.         unlink(cfname);
  341.     cfname[0] = '\0';
  342.     if ( (pcfile = gp_open_scratch_file(szScratch, cfname, "wb")) == (FILE *)NULL) {
  343.         play_sound(SOUND_ERROR);
  344.         return FALSE;
  345.     }
  346.     if (doc->numpages != 0)
  347.         psfile_extract(pcfile);
  348.     else {
  349.         dfreopen();
  350.         pscopyuntil(psfile.file, pcfile, doc->beginheader, doc->endtrailer, NULL);
  351.         dfclose();
  352.     }
  353.     fclose(pcfile);
  354.     fname = cfname;
  355.     }
  356.  
  357.     if (to_file) {
  358.     if (!get_filename(output, TRUE, FILTER_ALL, IDS_OUTPUTFILE, IDS_TOPICPRINT))
  359.         return FALSE;
  360.     }
  361. #else
  362.     psname[0] = '\0';
  363.     if ( (pcfile = gp_open_scratch_file(szScratch, psname, "wb")) == (FILE *)NULL) {
  364.     gserror(IDS_NOTEMP, NULL, MB_ICONEXCLAMATION, SOUND_ERROR);
  365.     play_sound(SOUND_ERROR);
  366.     return FALSE;
  367.     }
  368.  
  369.     if (doc == (PSDOC *)NULL) {
  370.     /* copy non-DSC file */
  371.     char *buffer;
  372.     int count;
  373.     /* create buffer for PS file copy */
  374.     buffer = malloc(COPY_BUF_SIZE);
  375.     if (buffer == (char *)NULL) {
  376.         play_sound(SOUND_ERROR);
  377.         fclose(pcfile);
  378.         unlink(psname);
  379.         return FALSE;
  380.     }
  381.     while ( (count = fread(buffer, 1, COPY_BUF_SIZE, psfile.file)) != 0 ) {
  382.         fwrite(buffer, 1, count, pcfile);
  383.     }
  384.     free(buffer);
  385.     }
  386.     else {
  387. /* A temporary DSC file already exists so don't bother with this.
  388.     if (psfile.ispdf) {
  389.         if (!pdf_extract(pcfile)) {
  390.         fclose(pcfile);
  391.         return FALSE;
  392.         }
  393.     }
  394.     else
  395. */
  396.          {
  397.         /* copy DSC file */
  398.         if (doc->numpages != 0)
  399.         psfile_extract(pcfile);
  400.         else
  401.         pscopyuntil(psfile.file, pcfile, doc->beginheader, doc->endtrailer, NULL);
  402.     }
  403.     }
  404.  
  405.     fclose(pcfile);
  406.     
  407.     if (to_file || (strcmp(option.printer_port, "FILE:")==0)) {
  408.     if (!get_filename(output, TRUE, FILTER_ALL, IDS_OUTPUTFILE, IDS_TOPICPRINT))
  409.         return FALSE;
  410.     }
  411.     else {
  412.     strcpy(output, szSpoolPrefix);
  413.     strcat(output, option.printer_port);
  414.     }
  415.  
  416. #endif
  417.  
  418.     /* calculate image size */
  419.     switch (sscanf(option.device_resolution,"%fx%f", &print_xdpi, &print_ydpi)) {
  420.     case EOF:
  421.     case 0:
  422.         print_xdpi = print_ydpi = DEFAULT_RESOLUTION;
  423.         break;
  424.     case 1:
  425.         print_ydpi = print_xdpi;
  426.     }
  427.     i = get_paper_size_index();
  428.     if (i < 0) {
  429.     width = option.user_width;
  430.     height = option.user_height;
  431.     }
  432.     else {
  433.     width = papersizes[i].width;
  434.     height = papersizes[i].height;
  435.     }
  436.     width  = (unsigned int)(width  / 72.0 * print_xdpi + 0.5);
  437.     height = (unsigned int)(height / 72.0 * print_ydpi + 0.5);
  438.  
  439.     if ((optname[0] != '\0') && !debug)
  440.         unlink(optname);
  441.     optname[0] = '\0';
  442.     if ( (optfile = gp_open_scratch_file(szScratch, optname, "w")) == (FILE *)NULL) {
  443.         play_sound(SOUND_ERROR);
  444.         return FALSE;
  445.     }
  446.     if (option.gsversion == IDM_GS351)
  447.         fprintf(optfile, "-I\042%s\042\n", option.gsinclude);
  448.     else
  449.         fprintf(optfile, "-I%s\n", option.gsinclude);
  450.     fprintf(optfile, "-dNOPAUSE\n");
  451.     if (option.safer)
  452.     fprintf(optfile, "-dSAFER\n");
  453.     fprintf(optfile, "-sDEVICE=%s\n",option.device_name);
  454. #ifdef OLD
  455.     /* this version causes FIXEDMEDIA to be set in gs 3.51 */
  456.     /* which causes lots of configurationerror */
  457.     fprintf(optfile, "-r%gx%g\n", (double)print_xdpi, (double)print_ydpi);
  458.     fprintf(optfile, "-g%ux%u\n",width,height);
  459. #else
  460.     fprintf(optfile, "-dDEVICEXRESOLUTION=%g\n", (double)print_xdpi);
  461.     fprintf(optfile, "-dDEVICEYRESOLUTION=%g\n", (double)print_ydpi);
  462.     fprintf(optfile, "-dDEVICEWIDTH=%u\n", width);
  463.     fprintf(optfile, "-dDEVICEHEIGHT=%u\n", height);
  464. #endif
  465. /*
  466.     if (to_file) {
  467. */
  468.     fprintf(optfile, "-sOutputFile=");
  469.         if (option.gsversion == IDM_GS351)
  470.         fputc('\042', optfile);
  471.     for (p=output; *p != '\0'; p++)
  472.         if (*p == '\\')
  473.         fputc('/',optfile);
  474.         else
  475.         fputc(*p,optfile);
  476.         if (option.gsversion == IDM_GS351)
  477.         fputc('\042', optfile);
  478.     fputc('\n',optfile);
  479. /*
  480.     }
  481. */
  482.     if ((proplist = get_properties(option.device_name)) != (struct prop_item_s *)NULL) {
  483.     /* output current property selections */
  484.     for (i=0; proplist[i].name[0]; i++) {
  485.         if (strcmp(proplist[i].value, not_defined) != 0)
  486.         fprintf(optfile,"-%s=%s\n", proplist[i].name, proplist[i].value);
  487.     }
  488.     free((char *)proplist);
  489.     }
  490.  
  491.     /* PageOffset */
  492.     if (option.gsversion == IDM_GS351) {
  493.     strcpy(section, option.device_name);
  494.     strcat(section, " PageOffset");
  495.     if ( (prf = profile_open(szIniFile)) != (PROFILE *)NULL ) {
  496.         profile_read_string(prf, section, "X", "0", buf, sizeof(buf)-2);
  497.         if (sscanf(buf, "%f", &xoffset) != 1)
  498.         xoffset = 0;
  499.         profile_read_string(prf, section, "Y", "0", buf, sizeof(buf)-2);
  500.         if (sscanf(buf, "%f", &yoffset) != 1)
  501.         yoffset = 0;
  502.         profile_close(prf);
  503.     }
  504.     if ((xoffset != 0) || (yoffset != 0))
  505.         fprintf(optfile, "-c \042<< /PageOffset [%g %g] >> setpagedevice\042\n-f\n", 
  506.         (double)xoffset, (double)yoffset);
  507.     }
  508.  
  509.     if (option.gsversion == IDM_GS351)
  510.     fputc('\042', optfile);
  511.     for (p=psname; *p != '\0'; p++)
  512.     if (*p == '\\')
  513.         fputc('/',optfile);
  514.     else
  515.         fputc(*p,optfile);
  516.     if (option.gsversion == IDM_GS351)
  517.     fputc('\042', optfile);
  518.     fputs("\nquit.ps\n", optfile);
  519.     fclose(optfile);
  520.     return TRUE;
  521. }
  522.  
  523.